import mysql.connector


def connect_to_database():
    try:
        # 配置数据库连接参数
        config = {
            'user': 'test-root',
            'password': '',
            'host': 'localhost',
            'database': 'test',
            'port': '3316',
            'raise_on_warnings': True,

        }

        # 创建数据库连接
        connection = mysql.connector.connect(**config)
        print("成功连接到数据库!")

        # 创建游标对象
        cursor = connection.cursor()

        # 执行SQL查询
        cursor.execute("SELECT * FROM test_table")
        results = cursor.fetchall()

        for row in results:
            print(row)

        # 关闭游标和连接
        cursor.close()
        connection.close()

    except mysql.connector.Error as err:
        print(f"数据库连接失败: {err}")


if __name__ == "__main__":
    connect_to_database()